package com.dsbj.mp.icardservice.utils.encrypt.aes;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.util.Base64;

public class AESencrypt_2 {

	/**
	 * 加密
	 * @param content
	 * @param strKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] encrypt(String content, String strKey) throws Exception {
		SecretKeySpec skeySpec = getKey(strKey);
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes("utf-8"));
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
		byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
		return encrypted;
	}

	/**
	 * 解密
	 * 
	 * @param strKey
	 * @param content
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(byte[] content, String strKey) throws Exception {
		SecretKeySpec skeySpec = getKey(strKey);
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes("utf-8"));
		cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
		byte[] original = cipher.doFinal(content);
		String originalString = new String(original, "utf-8");
		return originalString;
	}

	private static SecretKeySpec getKey(String strKey) throws Exception {
		byte[] arrBTmp = strKey.getBytes("utf-8");
		byte[] arrB = new byte[16]; // 创建一个空的16位字节数组（默认值为0）

		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}
		SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");
		return skeySpec;
	}

	/**
	 * base 64 encode
	 * 
	 * @param bytes
	 *            待编码的byte[]
	 * @return 编码后的base 64 code
	 */
	public static String base64Encode(byte[] bytes) {
		// new BASE64Encoder().encode(bytes);
		//StandardCharsets.ISO_8859_1
		return Base64.getEncoder().encodeToString(bytes);
	}

	/**
	 * base 64 decode
	 * 
	 * @param base64Code
	 *            待解码的base 64 code
	 * @return 解码后的byte[]
	 * @throws IOException
	 * @throws Exception
	 */
	public static byte[] base64Decode(String base64Code) throws IOException {
		//return base64Code.isEmpty() ? null : new BASE64Decoder().decodeBuffer(base64Code);
		//StandardCharsets.ISO_8859_1
		return base64Code.isEmpty() ? null: Base64.getDecoder().decode(base64Code);
	}

	/**
	 * AES加密为base 64 code
	 * 
	 * @param content
	 *            待加密的内容
	 * @param encryptKey
	 *            加密密钥
	 * @return 加密后的base 64 code
	 * @throws Exception
	 */
	public static String aesEncrypt(String content, String encryptKey) throws Exception {
		return base64Encode(encrypt(content, encryptKey));
	}

	/**
	 * 将base 64 code AES解密
	 * 
	 * @param encryptStr
	 *            待解密的base 64 code
	 * @param decryptKey
	 *            解密密钥
	 * @return 解密后的string
	 * @throws Exception
	 */
	public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
		return encryptStr.isEmpty() ? null : decrypt(base64Decode(encryptStr), decryptKey);
	}

	/**
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String content = "渔歌互答，此乐何极。。渔歌互答，此乐何极。。渔歌互答，此乐何极。。渔歌互答，此乐何极。。";
		String key = "仲淹仲淹仲淹仲淹";
		
		System.out.println("加密前：" + content);
		System.out.println("密钥：" + key);

		String encrypt = aesEncrypt(content, key);
		System.out.println("加密后：" + encrypt);

		String decrypt = aesDecrypt(encrypt, key);
		System.out.println("解密后：" + decrypt);
		
	}

}
